package service

import (
	"context"
	"fmt"
	__ "member/member-srv/basic/proto"
	"member/member-srv/handler/models"
)

type Server struct {
	__.UnimplementedMemberServer
}

// SayHello implements helloworld.GreeterServer
func (s *Server) MemberPlanAdd(_ context.Context, in *__.MemberPlanAddReq) (*__.MemberPlanAddResp, error) {
	member := models.MemberPlan{
		PlanName:  in.PlanName,
		PlanPrice: in.PlanPrice,
		Day:       in.Day,
	}
	if err := models.MemberPlanAdd(&member); err != nil {
		return nil, fmt.Errorf("实现失败")
	}
	return &__.MemberPlanAddResp{
		Id: int64(member.ID),
	}, nil
}
func (s *Server) MemberPlanList(_ context.Context, in *__.MemberPlanListReq) (*__.MemberPlanListResp, error) {
	var member models.MemberPlan
	if err := models.MemberPlanList(&member, int(in.Id)); err != nil {
		return nil, fmt.Errorf("查询失败")
	}
	return &__.MemberPlanListResp{
		PlanName:  member.PlanName,
		PlanPrice: member.PlanPrice,
		Day:       member.Day,
	}, nil
}
func (s *Server) OrderAdd(_ context.Context, in *__.OrderAddReq) (*__.OrderAddResp, error) {
	order := models.Order{
		UserId:       int(in.UserId),
		MemberPlanId: int(in.MemberPlanId),
		Name:         in.Name,
		Price:        in.Price,
		Number:       in.Number,
	}
	if err := models.OrderAdd(&order); err != nil {
		return nil, fmt.Errorf("添加失败")
	}
	return &__.OrderAddResp{
		Id: int64(order.ID),
	}, nil
}
func (s *Server) OrderList(_ context.Context, in *__.OrderListReq) (*__.OrderListResp, error) {
	var order models.Order
	if err := models.OrderList(&order, int(in.Id)); err != nil {
		return nil, fmt.Errorf("查询失败")
	}
	return &__.OrderListResp{
		UserId:       int64(order.UserId),
		MemberPlanId: int64(order.MemberPlanId),
		Name:         order.Name,
		Price:        order.Price,
		Number:       order.Number,
	}, nil
}
